package com.iyjhsoc.game.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.iyjhsoc.game.ENUM.MethodsENUM;
import com.iyjhsoc.game.dao.*;
import com.iyjhsoc.game.domain.VO.UtilVO;
import com.iyjhsoc.game.domain.entity.*;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.UtilService;
import com.iyjhsoc.game.util.GameFill;
import com.iyjhsoc.game.util.HttpUtils;
import com.iyjhsoc.game.util.LPLRequest;
import com.iyjhsoc.game.util.RequestBuilder;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController

@RequestMapping("iyjhsoc/util")
public class UtilController {
    @Value("${url.kpl.battle}")
    String kplUrl;
    @Value("${url.kpl.matchTime}")
    String matchTimeUrl;

    @Value("${url.kgl.matchCourse}")
    String matchCourseUrl;

    @Value("${url.kpl.matchCourse}")
    String kplMatchCourse;
    @Resource
    CompetitionDAO competitionDAO;
    @Resource
    CompetitionSmallGameDAO competitionSmallGameDAO;
    @Resource
    TeamDAO teamDAO;
    @Resource
    UtilService utilService;

    @GetMapping("getCompetitionData")
    @ApiModelProperty("抓取BiBiLiLi对局结算信息")
    public Response getCompetitionData(String competitionId,String url){
        String data = HttpUtils.get(url);
        Response competitionData = utilService.getCompetitionData(data, competitionId);
        return competitionData;
    }

    @GetMapping("getKpLCompetition")
    @ApiModelProperty("抓取KPL的对局信息")
    public Response getKpLCompetition(String matchId,String url){
        // https://prod.comp.smoba.qq.com/leaguesite/matches/open?league_id=20240003
        String data = HttpUtils.get(url);
        Response response = utilService.getKpLCompetition(data,matchId);
        return response;
    }

    @Value("${url.kpl.matchTime}")
    public String kplMatchIdUrl;
    @Value("${url.kpl.battle}")
    public String battleIdUrl;

    @GetMapping("getKplSmallGame")
    public Response getKplSmallGame(@Param("startCompetitionId") String startCompetitionId,@Param("endCompetitionId") String endCompetitionId){
        List<CompetitionDO> competitionDOS = competitionDAO.queryListBetweenIds(startCompetitionId, endCompetitionId);
        for(CompetitionDO competitionDO:competitionDOS){
            String s = HttpUtils.get(String.format(kplMatchIdUrl, competitionDO.getOutNet()));
            Response<List<CompetitionSmallGameDO>> battleId = utilService.getBattleId(s, competitionDO);
            for(CompetitionSmallGameDO smallGameDO:battleId.getData()) {
                String s1 = HttpUtils.get(String.format(battleIdUrl, smallGameDO.getBattleId()));
                utilService.insertKplSmallGame(s1,smallGameDO);
            }
        }
        return Response.SUCCESS();
    }

    @GetMapping("getLPLMatchInfo")
    public Response getLPLMatchInfo(String matchId,String url){
        String s = HttpUtils.get(url);
        Response lplMatchInfo = utilService.getLPLMatchInfo(matchId, s);
        return lplMatchInfo;
    }

    @Value("${url.lpl.matchId}")
    public String lplMatchId;
    @GetMapping("getLPLMatchDetailInfo")
    public Response getLPLMatchDetailInfo(String startId,String endId){
        List<CompetitionDO> competitionDOS = competitionDAO.queryListBetweenIds(startId, endId);
        for(CompetitionDO competitionDO: competitionDOS){
            String s = HttpUtils.get(String.format(lplMatchId, competitionDO.getOutNet()), LPLRequest.createRequestHeader(competitionDO.getOutNet()));
            System.out.println("返回结果===>"+s);
        }
        return null;
    }

}
